package cn.com.stary1993.delayqueue.redis.controller;

import cn.com.stary1993.delayqueue.redis.support.RedisDelayQueue;
import cn.hutool.core.date.DateUtil;
import cn.com.stary1993.delayqueue.redis.model.entities.Order;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@RestController
public class RedisDelayQueueController {
    @Resource
    private RedisDelayQueue redisDelayQueue;

    @GetMapping("/push/{sec}")
    public String push(@PathVariable  long sec) throws Exception {
        Random random = new Random();
        for (int i = 1; i <= 2; i++) {
            Map<String, Object> data = new HashMap<>();
            data.put("name", "张三" + i);
            data.put("age", "20");
            data.put("sex", "男");
            data.put("date", DateUtil.now());
            Long currentTime=System.currentTimeMillis();
            Long time = System.currentTimeMillis() + sec * 1000;
            data.put("expireTime",DateUtil.format(new Date(time),"yyyy-MM-dd HH:mm:ss"));
            data.put("timeOut",time-currentTime);
            redisDelayQueue.push("1", data, time);
        }
        // 模拟下单超时
        for (int i = 1; i <= 2; i++) {
            Order order=new Order();
            order.setOrderId(1l);
            order.setUserId(101l);
            order.setGoodsId(531l);
            order.setGoodsName("小米📱"+i);
            order.setPrice(4100);
            order.setDate(DateUtil.now());
            Long currentTime=System.currentTimeMillis();
            Long time = System.currentTimeMillis() + random.nextInt(10000 - 1000) + 1000 + 1;
            order.setExpireTime(DateUtil.format(new Date(time),"yyyy-MM-dd HH:mm:ss"));
            order.setTimeOut(time-currentTime);
            redisDelayQueue.push("2", order, time);
        }
        return "成功";
    }

}
